﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Dycox.Windows.Forms
{
    public class DetailsViewCollectionPropertyItem : DetailsViewPropertyItem
    {
        public DetailsViewCollectionPropertyItem(PropertyDescriptor property, object instance)
            : base(property, instance)
        {

        }

        protected override string GetValueText()
        {
            if (Expandable)
            {
                return string.Format("共{0}项", Collection.Count);
            }
            else
                return "<无>";
        }

        protected override void PopulateItems(List<DetailsViewItem> list)
        {
            if (Expandable)
            {
                int index = 0;
                foreach (object obj in Collection)
                {
                    list.Add(new DetailsViewCollectionElementItem(obj, index++));
                }
            }
        }

        private ICollection Collection
        {
            get { return (ICollection)Value; }
        }

        public override bool Expandable
        {
            get
            {
                var coll = this.Collection;

                return coll != null && coll.Count > 0;
            }
        }
    }
}
